package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AppAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.ActivityApkManageBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class ApkManageActivity extends BaseAc<ActivityApkManageBinding> {

    private AppAdapter appAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_apk_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivApkManageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvApkList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        appAdapter = new AppAdapter();
        mDataBinding.rvApkList.setAdapter(appAdapter);
        appAdapter.addChildClickViewIds(R.id.ivAppItemEdit);
        appAdapter.setOnItemChildClickListener(this);
        appAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        AppUtils.launchAppDetailsSettings(appAdapter.getItem(position).pkgName);
    }

    @Override
    protected void initData() {
        loadApks();
    }

    private void loadApks() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<String>>() {
            @Override
            public void doBackground(ObservableEmitter<List<String>> emitter) {
                emitter.onNext(AeUtil.getNoSystemInstalledPackageNames());
            }

            @Override
            public void accept(List<String> packageNames) {
                if (!CollectionUtils.isEmpty(packageNames)) {
                    mDataBinding.rvApkList.setVisibility(View.VISIBLE);
                    mDataBinding.tvApkManageNoData.setVisibility(View.GONE);
                    List<ApkBean> apkBeans = new ArrayList<>();
                    ApkBean bean;
                    for (String packageName : packageNames) {
                        bean = new ApkBean();
                        bean.pkgName = packageName;
                        apkBeans.add(bean);
                    }
                    appAdapter.setList(apkBeans);
                } else {
                    mDataBinding.rvApkList.setVisibility(View.GONE);
                    mDataBinding.tvApkManageNoData.setVisibility(View.VISIBLE);
                }
                dismissDialog(500L);
            }
        });
    }

}